package okhttp3;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Proxy;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.function.Consumer;

public class OkHttpTest {
    private static String fileUploadPath = "F:\\DownLoad\\smile.png";

    public static void main(String[] args) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .proxy(Proxy.NO_PROXY)
                .build();

        try {
            InetAddress[] addresses = InetAddress.getAllByName("www.baidu.com");
            Arrays.stream(addresses).forEach(new Consumer<InetAddress>() {
                @Override
                public void accept(InetAddress inetAddress) {
                    System.out.println(inetAddress);
                }
            });
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        RequestBody requestBody = FormBody.create(MediaType.parse("image/*"), new File(fileUploadPath));
        RequestBody uploadImgRequestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", "smile.png", RequestBody.create(MediaType.parse("application/octet-stream"), new File(fileUploadPath)))
                .addFormDataPart("s", "App.CDN.UploadImg")
                .addFormDataPart("app_key", "7CE6083321AF62B12F89B18B1CCE36C0")
                .build();
        Request request = new Request.Builder()
                .url("http://hb5.api.okayapi.com/")
                .method("POST", uploadImgRequestBody)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println(response.body().string());
            }
        });

    }
}
